/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * RSProjectionTechniqueElement.cpp
 *
 *  Created on: 2010/03/09
 *      Author: Alexandre Pires ( c.alexandre.pires@gmail.com )
 */

#include "RSProjectionTechniqueElement.h"
#include "RSValueElement.h"
#include "RSFloat.h"

namespace Realsphere {

    RSProjectionTechniqueElement::RSProjectionTechniqueElement ( RSProjectionElementType rProjectionElementType, const QString& rScopeId ) :
        RSTechniqueElement ( ProjectionElementTypeToTag ( rProjectionElementType ), rScopeId )
    {
        mProjectionElementType = rProjectionElementType;
        mAspectRatio = NULL;
        mZNear = NULL;
        mZFar = NULL;
    }

    RSProjectionTechniqueElement::~RSProjectionTechniqueElement ()
    {
        mAspectRatio = NULL;
        mZNear = NULL;
        mZFar = NULL;
    }

    RSFloat* RSProjectionTechniqueElement::AspectRatio () const
    {
        return mAspectRatio;
    }

    RSFloat* RSProjectionTechniqueElement::ZNear () const
    {
        return mZNear;
    }

    RSFloat* RSProjectionTechniqueElement::ZFar () const
    {
        return mZFar;
    }

    RSProjectionElementType RSProjectionTechniqueElement::ProjectionType () const
    {
        return mProjectionElementType;
    }

    bool RSProjectionTechniqueElement::IsRootElement () const
    {
        return false;
    }

    RSError RSProjectionTechniqueElement::SetAspectRatio ( RSFloat* rValue )
    {
        rValue->SetTag ( RS_ASPECT_RATIO_TAG );
        RSError error = AddValueElement ( rValue );
        return error;
    }

    RSError RSProjectionTechniqueElement::SetZNear ( RSFloat* rValue )
    {
        rValue->SetTag ( RS_ZNEAR_TAG );
        RSError error = AddValueElement ( rValue );
        return error;
    }

    RSError RSProjectionTechniqueElement::SetZFar ( RSFloat* rValue )
    {
        rValue->SetTag ( RS_ZFAR_TAG );
        RSError error = AddValueElement ( rValue );
        return error;
    }

    RSError RSProjectionTechniqueElement::SetAspectRatio ( const RSFloat& rValue )
    {
        if ( mAspectRatio == NULL )
            return RS_VALUE_UNDEFINED;

        mAspectRatio->SetValue ( rValue );

        return RS_NO_ERROR;
    }

    RSError RSProjectionTechniqueElement::SetZNear ( const RSFloat& rValue )
    {
        if ( mZNear == NULL )
            return RS_VALUE_UNDEFINED;

        mZNear->SetValue ( rValue );

        return RS_NO_ERROR;
    }

    RSError RSProjectionTechniqueElement::SetZFar ( const RSFloat& rValue )
    {
        if ( mZFar == NULL )
            return RS_VALUE_UNDEFINED;

        mZFar->SetValue ( rValue );

        return RS_NO_ERROR;
    }

    RSError RSProjectionTechniqueElement::SetAspectRatio ( const float& rValue )
    {
        RSFloat value ( rValue );
        RSError error = SetAspectRatio ( value );
        return error;
    }

    RSError RSProjectionTechniqueElement::SetZNear ( const float& rValue )
    {
        RSFloat value ( rValue );
        RSError error = SetZNear ( value );
        return error;
    }

    RSError RSProjectionTechniqueElement::SetZFar ( const float& rValue )
    {
        RSFloat value ( rValue );
        RSError error = SetZFar ( value );
        return error;
    }

    RSError RSProjectionTechniqueElement::BeforeAddValueElementValidation ( RSValueElement* rValueElement )
    {
        RSError
                error =
                        RSTechniqueElement::BeforeAddValueElementValidation ( rValueElement );

        if ( error == RS_INVALID_VALUE )
        {
            if ( rValueElement->Tag () == RS_ASPECT_RATIO_TAG
                    || rValueElement->Tag () == RS_ZNEAR_TAG
                    || rValueElement->Tag () == RS_ZFAR_TAG )
            {
                error = RS_NO_ERROR;
            }
        }

        return error;
    }

    RSError RSProjectionTechniqueElement::BeforeRemoveValueElementValidation ( RSValueElement* rValueElement )
    {
        RSError
                error =
                        RSTechniqueElement::BeforeRemoveValueElementValidation ( rValueElement );

        if ( error == RS_INVALID_VALUE )
        {

            if ( rValueElement->Tag () == RS_ASPECT_RATIO_TAG
                    || rValueElement->Tag () == RS_ZNEAR_TAG
                    || rValueElement->Tag () == RS_ZFAR_TAG )
            {
                error = RS_NO_ERROR;
            }
        }

        return error;
    }

    bool RSProjectionTechniqueElement::AfterAddValueElementValidation ( RSValueElement* rValueElement )
    {
        bool
                handled =
                        RSTechniqueElement::AfterAddValueElementValidation ( rValueElement );

        if ( !handled )
        {
            RSFloat* value = RSFloatCast ( rValueElement );

            if ( rValueElement->Tag () == RS_ASPECT_RATIO_TAG )
            {
                if ( mAspectRatio != NULL )
                    RemoveChildElement ( mAspectRatio );

                mAspectRatio = value;
                handled = true;

                emit AspectRatioAdded ( mAspectRatio );
            }
            else if ( rValueElement->Tag () == RS_ZNEAR_TAG )
            {
                if ( mZNear != NULL )
                    RemoveChildElement ( mZNear );

                mZNear = value;
                handled = true;

                emit ZNearAdded ( mZNear );
            }
            else if ( rValueElement->Tag () == RS_ZFAR_TAG )
            {
                if ( mZFar != NULL )
                    RemoveChildElement ( mZFar );

                mZFar = value;
                handled = true;

                emit ZFarAdded ( mZFar );
            }
        }
        return handled;
    }

    bool RSProjectionTechniqueElement::AfterRemoveValueElementValidation ( RSValueElement* rValueElement )
    {
        bool
                handled =
                        RSTechniqueElement::AfterAddValueElementValidation ( rValueElement );

        if ( !handled )
        {

            if ( rValueElement->Tag () == RS_ASPECT_RATIO_TAG )
            {
                mAspectRatio = NULL;
                handled = true;
                emit AspectRatioRemoved ( mAspectRatio );
            }
            else if ( rValueElement->Tag () == RS_ZNEAR_TAG )
            {
                mZNear = NULL;
                handled = true;
                emit ZNearRemoved ( mZNear );
            }
            else if ( rValueElement->Tag () == RS_ZFAR_TAG )
            {
                mZFar = NULL;
                handled = true;
                emit ZFarRemoved ( mZFar );
            }
        }

        return handled;
    }

    QString RSProjectionTechniqueElement::ProjectionElementTypeToTag ( RSProjectionElementType rProjectionElementType )
    {
        switch ( rProjectionElementType )
        {
            case RS_ORTOGRAPHIC:
                return QString ( RS_ORTHOGRAPHIC_TAG );
                break;
            case RS_PERSPECTIVE:
                return QString ( RS_PERSPECTIVE_TAG );
                break;
            default:
                return QString ( RS_ELEMENT_TAG );
                break;
        }
    }

    RSProjectionElementType RSProjectionTechniqueElement::ProjectionElementTagToType ( const QString& rTag )
    {

        if ( rTag == RS_ORTHOGRAPHIC_TAG )
        {
            return RS_ORTOGRAPHIC;
        }
        else if ( rTag == RS_PERSPECTIVE_TAG )
        {
            return RS_PERSPECTIVE;
        }

        return RS_PROJECTION_ELEMENT_UNKNOWN;
    }
}
